package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;



    @Override
    public Page<RoomItemVo> pageRoomItemVoList(Page<RoomItemVo> roomItemVoPage, RoomQueryVo queryVo) {
        //查询房间所属公寓的基本信息



        return baseMapper.pageRoomItemVoListMapper(roomItemVoPage,queryVo);
    }

    //根据id获取房间详细信息
    @Override
    public RoomDetailVo getRoomDetailVoList(Long id) {
        //获取房间的基本信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //获取所属公寓信息
        Long apartmentId = roomInfo.getApartmentId();
       ApartmentItemVo apartmentItemVoList = apartmentInfoService.getApartmentItemVoListByRoomId(apartmentId);
       //获得房间图片信息
        List<GraphVo> graphVoList = graphInfoMapper.getRoomGrapgInfoMapperListByRoomId(id,ItemType.ROOM);
        //根据房间id获取属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.getAttrValueMapperListByRoomId(id);
        //根据房间id获取配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoMapperListByRoomId(id);
        //获取标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoMapperListByRoomId(id);
        //获取支付方式信息列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPayMentTypeMapperListByRoomId(id);
        //获取杂费(公寓id)
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueMapperListByRoomId(apartmentId);
        //租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermMapperListByRoomId(id);


        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //房间是否被删除
        roomDetailVo.setIsDelete(roomInfo.getIsDeleted() == 1);
        //房间是否已入住
//        LeaseAgreement leaseAgreement = new LeaseAgreement();
//        leaseAgreement.setRoomId(id);
//        LeaseStatus status = leaseAgreement.getStatus();
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId,roomInfo.getId());
        queryWrapper.in(LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        Long count = leaseAgreementMapper.selectCount(queryWrapper);
        //转换成roomDetailVo
//        roomDetailVo.setIsCheckIn(status == LeaseStatus.SIGNED ||
//                status == LeaseStatus.WITHDRAWING ||
//                status == LeaseStatus.RENEWING);
        roomDetailVo.setIsCheckIn(count>0);
        //设置公寓信息
        roomDetailVo.setApartmentItemVo(apartmentItemVoList);
        //设置图片列表
        roomDetailVo.setGraphVoList(graphVoList);
        //设置属性信息列表
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        //设置配套信息列表
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        //设置标签信息列表
        roomDetailVo.setLabelInfoList(labelInfoList);
        //设置支付方式
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //设置杂费
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        //设置租期
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }
}




